package com.shop.controller.user;

import com.shop.entity.ShopProduct;
import com.shop.entity.ShopUser;
import com.shop.service.ShopProductService;
import com.shop.service.ShopUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import java.util.List;

@Controller
public class RegisterController {

    @Autowired
    ShopUserService shopUserService;
    @Autowired
    ShopProductService shopProductService;

    @PostMapping("/registration")
    public String registration(Model model, String username, String password) {

        ShopUser shopUser = shopUserService.getShopUserByUsername(username);
        //int lastId = shopUserService.getLastUserId();

        if(shopUser == null) {
            ShopUser newUser = new ShopUser(username, password);
            shopUserService.addShopUser(newUser);
            System.out.println("注册成功！");
            return "redirect:login.html";
        } else{
            System.out.println("用户名已存在！");
            return "redirect:registration.html";
        }
    }

}
